|
|
|
|
|
|
|
In all the examples so far, you have used the SELECT_INTO statement to copy existing tables to another table within the database. You can also use SELECT_INTO to copy an existing table to another database by adding the IN clause. You can use this feature to copy entire data tables from one database to another, or to copy portions of a database or data tables to another database for archiving or reporting purposes. |
|
|
|
|
|
|
|
|
For example, if you want to copy the entire BaseTable you designed in the previous examples from SQLVB07.MDB to SQLVB07B.MDB, you could use the following SELECT_INTO statement: |
|
|
|
|
|
|
|
|
SELECT * INTO CopyTable IN sqlvb07b.mdb FROM BaseTable; |
|
|
|
|
|
|
|
|
You can use all the WHERE, ORDER BY, GROUP BY, HAVING, and AS clauses you desire when copying tables from one database to another. |
|
|
|
|
| |
|
|
|
|
When you copy tables using the SELECT_INTO statement, none of the indexes or constraints are copied to the new table. This is an important point. If you use SELECT_INTO to create tables that you want to use for data entry, you need to reconstruct the indexes and constraints using CREATE INDEX to add indexes and ALTER TABLE to add constraints. |
|
|
|
|
|
|
|
|
|
|
Creating Delete Table Queries Using DELETE_FROM |
|
|
|
|
|
|
|
|
The final SQL statement you learn today is the DELETE_FROM statement, commonly called the Delete Table query. Delete Table queries are used to remove one or more records from a data table. The delete query can also be applied to a valid view created using the JOIN keyword. Although it is not always efficient to use the DELETE statement to remove a single record from a table, it can be very effective to use the DELETE statement to remove several records from a table. In fact, when you need to remove more than one record from a table or view, the DELETE statement outperforms repeated uses of the Delete method in Visual Basic code. |
|
|
|
|
|
|
|
|
In its most basic form, the DELETE statement looks like this: |
|
|
|
|
|
|
|
|
In the preceding example, TableName represents the name of the base table from which you are deleting records. In this case, all records in the table are removed using a single command. If you want to remove only some of the records, you could add an SQL WHERE clause to limit the scope of the DELETE action. |
|
|
|
|
|
|
|
|
DELETE FROM TableName WHERE Field = value; |
|
|
|
|
|
|
|
|
This example removes only the records that meet the criteria established in the WHERE clause. |
|
|
|
|
|